Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 639)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.04159 13.03753 13.03356 13.02967 13.02584 13.02209 13.01840 13.01477
## [9] 13.01119 13.00765 13.00416 13.00071 12.99729 12.99389 12.99052 12.98716
## [17] 12.98381 12.98047 12.97712 12.97377 12.97042 12.96704 12.96365 12.96023
## [25] 12.95678 12.95329 12.94976 12.94619 12.94256 12.93890 12.93522 12.93154
## [33] 12.92786 12.92418 12.92050 12.91682 12.91316 12.90951 12.90588 12.90227
## [41] 12.89868 12.89513 12.89160 12.88812 12.88467 12.88126 12.87790 12.87459
## [49] 12.87134 12.86814 12.86500 12.86193 12.85892 12.85599 12.85313 12.85035
## [57] 12.84765 12.84504 12.84251 12.84008 12.83775 12.83552 12.83339 12.83137
## [65] 12.82937 12.82734 12.82526 12.82315 12.82101 12.81886 12.81669 12.81452
## [73] 12.81234 12.81018 12.80803 12.80590 12.80380 12.80174 12.79972 12.79774
## [81] 12.79583 12.79397 12.79218 12.79047 12.78884 12.78730 12.78586 12.78452
## [89] 12.78329 12.78217 12.78118 12.78032 12.77959 12.77901 12.77858 12.77830
## [97] 12.77819 12.77825 12.77849 12.77873 12.77880 12.77872 12.77850 12.77816
## [105] 12.77772 12.77719 12.77658 12.77592 12.77521 12.77448 12.77374 12.77300
## [113] 12.77228 12.77160 12.77097 12.77041 12.76994 12.76956 12.76929 12.76916
## [121] 12.76918 12.76935 12.76971 12.77026 12.77101 12.77199 12.77322 12.77470
## [129] 12.77645 12.77849 12.78083 12.78349 12.78649 12.79092 12.79769 12.80648
## [137] 12.81696 12.82882 12.84175 12.85543 12.86955 12.88378 12.89782 12.91134
## [145] 12.92403 12.93558 12.94567 12.95399 12.96021 12.96668 12.97574 12.98708
## [153] 13.00041 13.01542 13.03179 13.04923 13.06742 13.08606 13.10485 13.12348
## [161] 13.14164 13.15903 13.17534 13.19027 13.20350 13.21474 13.22368 13.23000
## [169] 13.23547 13.24196 13.24937 13.25759 13.26651 13.27603 13.28605 13.29645
## [177] 13.30712 13.31797 13.32888 13.33976 13.35048 13.36095 13.37107 13.38071
## [185] 13.38978 13.39818 13.40578 13.41250 13.41821 13.42282 13.42622 13.42830
## [193] 13.42896 13.42808 13.42557 13.42131 13.41527 13.40759 13.39842 13.38792
## [201] 13.37626 13.36358 13.35005 13.33583 13.32108 13.30594 13.29059 13.27518
## [209] 13.25986 13.24480 13.23016 13.21609 13.20046 13.18129 13.15894 13.13381
## [217] 13.10626 13.07668 13.04546 13.01296 12.97956 12.94565 12.91161 12.87781
## [225] 12.84464 12.81247 12.78168 12.75265 12.72576 12.70139 12.67992 12.65803
## [233] 12.63251 12.60388 12.57270 12.53947 12.50475 12.46905 12.43292 12.39688
## [241] 12.36148 12.32723 12.29468 12.26436 12.23680 12.21253 12.19208 12.17430
## [249] 12.15760 12.14191 12.12713 12.11318 12.09997 12.08741 12.07543 12.06392
## [257] 12.05282 12.04202 12.03145 12.02102 12.01064 12.00022 11.98968 11.97894
## [265] 11.96790 11.95648 11.94639 11.93915 11.93438 11.93170 11.93074 11.93112
## [273] 11.93247 11.93441 11.93656 11.93856 11.94001 11.94055 11.93980 11.93738
## [281] 11.93292 11.92604 11.91830 11.91143 11.90532 11.89988 11.89499 11.89056
## [289] 11.88648 11.88266 11.87898 11.87534 11.87165 11.86779 11.86367 11.85917
## [297] 11.85421 11.84867 11.84245 11.83545 11.82756 11.81866 11.80879 11.79808
## [305] 11.78670 11.77477 11.76246 11.74991 11.73726 11.72467 11.71227 11.70022
## [313] 11.68866 11.67774 11.66761 11.65841 11.65028 11.64203 11.63239 11.62151
## [321] 11.60948 11.59642 11.58244 11.56767 11.55222 11.53619 11.51972 11.50290
## [329] 11.48586 11.46871 11.45156 11.43454 11.41775 11.40131 11.38533 11.36994
## [337] 11.35524 11.34135 11.32838 11.31645 11.30567 11.29617 11.28805 11.28142
## [345] 11.27641 11.27313 11.27169 11.27221 11.27480 11.27957 11.28657 11.29559
## [353] 11.30645 11.31894 11.33286 11.34802 11.36422 11.38125 11.39893 11.41704
## [361] 11.43539 11.45379 11.47202 11.48990 11.50723 11.52380 11.54205 11.56428
## [369] 11.59010 11.61912 11.65094 11.68516 11.72139 11.75924 11.79831 11.83821
## [377] 11.87855 11.91892 11.95894 11.99821 12.03633 12.07292 12.10757 12.13990
## [385] 12.16950 12.19599 12.21897 12.24174 12.26760 12.29615 12.32698 12.35967
## [393] 12.39384 12.42906 12.46493 12.50104 12.53699 12.57237 12.60678 12.63980
## [401] 12.67103 12.70006 12.72649 12.74991 12.76991 12.78609 12.80036 12.81482
## [409] 12.82935 12.84385 12.85819 12.87226 12.88595 12.89913 12.91170 12.92353
## [417] 12.93451 12.94453 12.95346 12.96119 12.96761 12.97260 12.97529 12.97508
## [425] 12.97224 12.96704 12.95977 12.95071 12.94012 12.92829 12.91549 12.90200
## [433] 12.88810 12.87406 12.86016 12.84668 12.83389 12.82207 12.81149 12.80244
## [441] 12.79519 12.78709 12.77558 12.76115 12.74424 12.72531 12.70484 12.68327
## [449] 12.66108 12.63871 12.61663 12.59531 12.57519 12.55675 12.54044 12.52673
## [457] 12.51607 12.50609 12.49429 12.48089 12.46613 12.45022 12.43340 12.41590
## [465] 12.39795 12.37978 12.36163 12.34371 12.32626 12.30951 12.29370 12.27904
## [473] 12.26577 12.25412 12.24432 12.23661 12.22960 12.22189 12.21363 12.20498
## [481] 12.19611 12.18717 12.17831 12.16971 12.16152 12.15390 12.14700 12.14099
## [489] 12.13602 12.13227 12.12987 12.12900 12.12985 12.13242 12.13654 12.14205
## [497] 12.14880 12.15663 12.16536 12.17486 12.18494 12.19546 12.20626 12.21717
## [505] 12.22803 12.23869 12.24898 12.25875 12.26783 12.27607 12.28330 12.29154
## [513] 12.30271 12.31648 12.33255 12.35060 12.37031 12.39136 12.41344 12.43624
## [521] 12.45943 12.48271 12.50575 12.52824 12.54987 12.57032 12.58927 12.60641
## [529] 12.62142 12.63398 12.64379 12.65052 12.65631 12.66341 12.67163 12.68082
## [537] 12.69081 12.70143 12.71252 12.72390 12.73541 12.74688 12.75815 12.76904
## [545] 12.77940 12.78904 12.79782 12.80555 12.81208 12.81723 12.82084 12.82273
## [553] 12.82275 12.82073 12.81650 12.80983 12.80077 12.78956 12.77644 12.76164
## [561] 12.74539 12.72794 12.70952 12.69037 12.67072 12.65081 12.63088 12.61116
## [569] 12.59189 12.57331 12.55565 12.53914 12.52403 12.51056 12.49750 12.48353
## [577] 12.46876 12.45325 12.43710 12.42039 12.40321 12.38564 12.36778 12.34970
## [585] 12.33148 12.31323 12.29502 12.27693 12.25906 12.24149 12.22387 12.20580
## [593] 12.18730 12.16840 12.14912 12.12946 12.10947 12.08915 12.06853 12.04762
## [601] 12.02645 12.00504 11.98341 11.96158 11.93956 11.91739 11.89508 11.87264
## [609] 11.85011 11.82745 11.80463 11.78164 11.75848 11.73514 11.71162 11.68793
## [617] 11.66406 11.64000 11.61575 11.59131 11.56669 11.54186 11.51684 11.49162
## [625] 11.46620 11.44057 11.41473 11.38868 11.36242 11.33594 11.30924 11.28233
## [633] 11.25518 11.22782 11.20022 11.17239 11.14432 11.11602 11.08748
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 639)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63150 12.62669 12.62199 12.61740 12.61292 12.60855 12.60427 12.60009
## [9] 12.59601 12.59202 12.58812 12.58430 12.58056 12.57690 12.57332 12.56982
## [17] 12.56638 12.56301 12.55970 12.55646 12.55327 12.55014 12.54705 12.54402
## [25] 12.54103 12.53809 12.53518 12.53231 12.52948 12.52668 12.52392 12.52120
## [33] 12.51853 12.51592 12.51335 12.51085 12.50840 12.50601 12.50369 12.50144
## [41] 12.49926 12.49715 12.49512 12.49317 12.49130 12.48952 12.48782 12.48622
## [49] 12.48471 12.48330 12.48198 12.48077 12.47967 12.47868 12.47779 12.47702
## [57] 12.47637 12.47584 12.47544 12.47516 12.47500 12.47499 12.47510 12.47535
## [65] 12.47573 12.47621 12.47680 12.47749 12.47829 12.47919 12.48020 12.48131
## [73] 12.48253 12.48386 12.48529 12.48683 12.48847 12.49022 12.49207 12.49404
## [81] 12.49611 12.49828 12.50056 12.50295 12.50545 12.50805 12.51076 12.51358
## [89] 12.51650 12.51953 12.52267 12.52592 12.52927 12.53274 12.53631 12.53999
## [97] 12.54377 12.54767 12.55167 12.55580 12.56008 12.56450 12.56905 12.57372
## [105] 12.57851 12.58342 12.58843 12.59353 12.59873 12.60401 12.60936 12.61479
## [113] 12.62028 12.62582 12.63141 12.63704 12.64270 12.64840 12.65411 12.65983
## [121] 12.66556 12.67130 12.67702 12.68273 12.68841 12.69407 12.69969 12.70527
## [129] 12.71134 12.71837 12.72625 12.73488 12.74415 12.75394 12.76416 12.77469
## [137] 12.78543 12.79626 12.80709 12.81779 12.82828 12.83843 12.84813 12.85729
## [145] 12.86580 12.87353 12.88040 12.88870 12.90050 12.91529 12.93258 12.95188
## [153] 12.97269 12.99451 13.01685 13.03922 13.06111 13.08204 13.10151 13.11903
## [161] 13.13409 13.14621 13.15488 13.16247 13.17156 13.18198 13.19359 13.20623
## [169] 13.21973 13.23394 13.24871 13.26386 13.27926 13.29473 13.31012 13.32528
## [177] 13.34004 13.35425 13.36775 13.38038 13.39198 13.40240 13.41149 13.41907
## [185] 13.42499 13.42911 13.43125 13.43126 13.42898 13.42448 13.41801 13.40972
## [193] 13.39976 13.38828 13.37541 13.36131 13.34613 13.33001 13.31309 13.29553
## [201] 13.27747 13.25905 13.24043 13.22174 13.20314 13.18478 13.16679 13.14933
## [209] 13.13254 13.11656 13.09857 13.07603 13.04957 13.01982 12.98741 12.95296
## [217] 12.91711 12.88047 12.84367 12.80735 12.77212 12.73863 12.70748 12.67931
## [225] 12.65475 12.63443 12.61512 12.59340 12.56957 12.54393 12.51679 12.48847
## [233] 12.45925 12.42945 12.39937 12.36931 12.33959 12.31051 12.28237 12.25547
## [241] 12.23013 12.20664 12.18532 12.16646 12.15038 12.13658 12.12425 12.11325
## [249] 12.10342 12.09459 12.08661 12.07934 12.07260 12.06624 12.06012 12.05406
## [257] 12.04791 12.04153 12.03474 12.02740 12.01935 12.01256 12.00890 12.00802
## [265] 12.00957 12.01323 12.01864 12.02545 12.03334 12.04195 12.05095 12.05999
## [273] 12.06872 12.07682 12.08392 12.08970 12.09380 12.09589 12.09562 12.09265
## [281] 12.08914 12.08732 12.08692 12.08770 12.08939 12.09174 12.09451 12.09742
## [289] 12.10022 12.10267 12.10450 12.10546 12.10530 12.10375 12.10057 12.09549
## [297] 12.08882 12.08109 12.07240 12.06287 12.05261 12.04171 12.03029 12.01846
## [305] 12.00632 11.99398 11.98155 11.96913 11.95684 11.94478 11.93306 11.92178
## [313] 11.91106 11.90100 11.89170 11.88060 11.86544 11.84681 11.82533 11.80159
## [321] 11.77619 11.74973 11.72282 11.69605 11.67003 11.64535 11.62262 11.60243
## [329] 11.58539 11.57210 11.56315 11.55554 11.54603 11.53492 11.52249 11.50902
## [337] 11.49480 11.48012 11.46526 11.45051 11.43616 11.42250 11.40980 11.39835
## [345] 11.38845 11.38038 11.37442 11.37086 11.36999 11.37209 11.37692 11.38391
## [353] 11.39285 11.40350 11.41566 11.42910 11.44362 11.45897 11.47496 11.49136
## [361] 11.50796 11.52452 11.54085 11.55671 11.57189 11.58617 11.60186 11.62116
## [369] 11.64373 11.66921 11.69725 11.72750 11.75960 11.79320 11.82794 11.86348
## [377] 11.89946 11.93553 11.97134 12.00652 12.04073 12.07362 12.10483 12.13401
## [385] 12.16080 12.18486 12.20583 12.22659 12.25004 12.27584 12.30363 12.33308
## [393] 12.36383 12.39554 12.42787 12.46047 12.49299 12.52510 12.55644 12.58666
## [401] 12.61544 12.64240 12.66723 12.68956 12.70905 12.72536 12.74054 12.75672
## [409] 12.77370 12.79123 12.80910 12.82708 12.84494 12.86248 12.87945 12.89564
## [417] 12.91082 12.92477 12.93726 12.94808 12.95699 12.96377 12.96862 12.97195
## [425] 12.97387 12.97446 12.97384 12.97211 12.96937 12.96572 12.96127 12.95612
## [433] 12.95036 12.94410 12.93745 12.93051 12.92337 12.91615 12.90893 12.90184
## [441] 12.89495 12.88584 12.87235 12.85508 12.83462 12.81155 12.78647 12.75997
## [449] 12.73263 12.70504 12.67778 12.65146 12.62666 12.60396 12.58395 12.56723
## [457] 12.55439 12.54222 12.52739 12.51024 12.49109 12.47029 12.44817 12.42508
## [465] 12.40135 12.37732 12.35332 12.32970 12.30679 12.28494 12.26447 12.24573
## [473] 12.22905 12.21477 12.20324 12.19478 12.18723 12.17834 12.16839 12.15761
## [481] 12.14628 12.13464 12.12296 12.11149 12.10049 12.09022 12.08092 12.07287
## [489] 12.06631 12.06150 12.05870 12.05817 12.05963 12.06257 12.06686 12.07238
## [497] 12.07901 12.08664 12.09513 12.10438 12.11425 12.12463 12.13540 12.14644
## [505] 12.15762 12.16883 12.17994 12.19084 12.20140 12.21151 12.22103 12.23248
## [513] 12.24814 12.26757 12.29034 12.31601 12.34414 12.37431 12.40608 12.43901
## [521] 12.47267 12.50662 12.54042 12.57365 12.60586 12.63663 12.66551 12.69207
## [529] 12.71588 12.73650 12.75349 12.76642 12.77836 12.79248 12.80853 12.82623
## [537] 12.84533 12.86558 12.88670 12.90844 12.93053 12.95272 12.97475 12.99634
## [545] 13.01725 13.03721 13.05597 13.07325 13.08880 13.10235 13.11366 13.12245
## [553] 13.12846 13.13144 13.13112 13.12769 13.12167 13.11331 13.10282 13.09045
## [561] 13.07643 13.06099 13.04437 13.02681 13.00853 12.98978 12.97078 12.95178
## [569] 12.93300 12.91468 12.89706 12.88036 12.86483 12.85070 12.83674 12.82161
## [577] 12.80541 12.78823 12.77018 12.75134 12.73181 12.71168 12.69104 12.67000
## [585] 12.64864 12.62707 12.60536 12.58363 12.56195 12.54043 12.51870 12.49631
## [593] 12.47331 12.44970 12.42550 12.40074 12.37544 12.34961 12.32327 12.29646
## [601] 12.26917 12.24145 12.21330 12.18474 12.15580 12.12650 12.09685 12.06688
## [609] 12.03660 12.00599 11.97501 11.94365 11.91191 11.87979 11.84729 11.81439
## [617] 11.78111 11.74744 11.71337 11.67890 11.64404 11.60877 11.57309 11.53701
## [625] 11.50051 11.46360 11.42628 11.38853 11.35037 11.31178 11.27276 11.23332
## [633] 11.19344 11.15313 11.11238 11.07119 11.02955 10.98747 10.94495
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 639)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.04453 12.03862 12.03284 12.02718 12.02165 12.01622 12.01091 12.00569
## [9] 12.00058 11.99556 11.99062 11.98577 11.98100 11.97630 11.97166 11.96709
## [17] 11.96258 11.95812 11.95370 11.94933 11.94499 11.94069 11.93641 11.93215
## [25] 11.92791 11.92368 11.91946 11.91523 11.91101 11.90677 11.90252 11.89825
## [33] 11.89395 11.88962 11.88526 11.88086 11.87645 11.87207 11.86772 11.86341
## [41] 11.85914 11.85492 11.85074 11.84661 11.84254 11.83854 11.83459 11.83072
## [49] 11.82692 11.82319 11.81955 11.81599 11.81252 11.80914 11.80586 11.80267
## [57] 11.79960 11.79663 11.79377 11.79103 11.78841 11.78592 11.78355 11.78132
## [65] 11.77922 11.77727 11.77546 11.77379 11.77228 11.77093 11.76973 11.76853
## [73] 11.76717 11.76567 11.76403 11.76229 11.76045 11.75854 11.75656 11.75454
## [81] 11.75250 11.75045 11.74840 11.74638 11.74440 11.74248 11.74064 11.73889
## [89] 11.73725 11.73573 11.73436 11.73315 11.73211 11.73128 11.73065 11.73025
## [97] 11.73010 11.73020 11.73059 11.73128 11.73227 11.73360 11.73527 11.73731
## [105] 11.73973 11.74255 11.74567 11.74897 11.75247 11.75616 11.76005 11.76414
## [113] 11.76843 11.77293 11.77763 11.78255 11.78768 11.79303 11.79860 11.80440
## [121] 11.81042 11.81666 11.82314 11.82986 11.83681 11.84400 11.85143 11.85911
## [129] 11.86703 11.87521 11.88364 11.89233 11.90128 11.91208 11.92604 11.94275
## [137] 11.96179 11.98276 12.00524 12.02881 12.05307 12.07760 12.10199 12.12583
## [145] 12.14870 12.17019 12.18988 12.20737 12.22225 12.23773 12.25705 12.27974
## [153] 12.30535 12.33344 12.36353 12.39519 12.42797 12.46140 12.49504 12.52843
## [161] 12.56112 12.59266 12.62260 12.65048 12.67585 12.69825 12.71725 12.73237
## [169] 12.74582 12.76002 12.77486 12.79023 12.80603 12.82215 12.83849 12.85492
## [177] 12.87136 12.88769 12.90380 12.91960 12.93496 12.94979 12.96397 12.97741
## [185] 12.98999 13.00160 13.01215 13.02151 13.02960 13.03629 13.04148 13.04507
## [193] 13.04695 13.04701 13.04514 13.04124 13.03458 13.02476 13.01214 12.99706
## [201] 12.97990 12.96100 12.94073 12.91944 12.89749 12.87524 12.85305 12.83128
## [209] 12.81028 12.79042 12.77204 12.75552 12.73755 12.71497 12.68828 12.65802
## [217] 12.62471 12.58887 12.55101 12.51167 12.47136 12.43061 12.38994 12.34987
## [225] 12.31092 12.27361 12.23848 12.20603 12.17679 12.15128 12.13003 12.10994
## [233] 12.08777 12.06384 12.03848 12.01200 11.98471 11.95695 11.92903 11.90126
## [241] 11.87398 11.84749 11.82211 11.79817 11.77599 11.75588 11.73816 11.72239
## [249] 11.70784 11.69441 11.68197 11.67043 11.65966 11.64955 11.64001 11.63091
## [257] 11.62214 11.61360 11.60517 11.59674 11.58820 11.57945 11.57036 11.56083
## [265] 11.55075 11.54000 11.53038 11.52350 11.51901 11.51653 11.51569 11.51614
## [273] 11.51751 11.51942 11.52152 11.52344 11.52482 11.52528 11.52446 11.52200
## [281] 11.51753 11.51068 11.50297 11.49610 11.48999 11.48453 11.47965 11.47525
## [289] 11.47124 11.46755 11.46408 11.46073 11.45743 11.45408 11.45060 11.44690
## [297] 11.44288 11.43847 11.43356 11.42808 11.42194 11.41437 11.40487 11.39371
## [305] 11.38117 11.36755 11.35311 11.33814 11.32293 11.30775 11.29288 11.27860
## [313] 11.26521 11.25297 11.24217 11.23309 11.22601 11.21951 11.21205 11.20371
## [321] 11.19459 11.18477 11.17435 11.16342 11.15206 11.14036 11.12842 11.11633
## [329] 11.10417 11.09204 11.08002 11.06821 11.05669 11.04556 11.03491 11.02481
## [337] 11.01538 11.00669 10.99883 10.99190 10.98598 10.98117 10.97755 10.97522
## [345] 10.97427 10.97477 10.97684 10.98055 10.98599 10.99326 11.00307 11.01586
## [353] 11.03128 11.04897 11.06859 11.08978 11.11220 11.13549 11.15932 11.18332
## [361] 11.20714 11.23044 11.25287 11.27408 11.29371 11.31142 11.32960 11.35065
## [369] 11.37429 11.40024 11.42819 11.45786 11.48895 11.52117 11.55424 11.58786
## [377] 11.62174 11.65559 11.68911 11.72202 11.75402 11.78482 11.81413 11.84167
## [385] 11.86713 11.89023 11.91067 11.93064 11.95235 11.97557 12.00004 12.02553
## [393] 12.05178 12.07857 12.10563 12.13274 12.15964 12.18609 12.21185 12.23667
## [401] 12.26032 12.28255 12.30311 12.32176 12.33826 12.35236 12.36537 12.37869
## [409] 12.39220 12.40580 12.41938 12.43282 12.44601 12.45885 12.47123 12.48302
## [417] 12.49414 12.50445 12.51386 12.52225 12.52952 12.53555 12.54008 12.54303
## [425] 12.54452 12.54469 12.54366 12.54158 12.53857 12.53476 12.53028 12.52527
## [433] 12.51987 12.51419 12.50838 12.50256 12.49687 12.49144 12.48640 12.48188
## [441] 12.47802 12.47321 12.46599 12.45665 12.44552 12.43289 12.41909 12.40442
## [449] 12.38919 12.37372 12.35831 12.34328 12.32894 12.31559 12.30355 12.29313
## [457] 12.28465 12.27627 12.26613 12.25443 12.24137 12.22718 12.21204 12.19619
## [465] 12.17981 12.16313 12.14635 12.12967 12.11331 12.09748 12.08238 12.06822
## [473] 12.05522 12.04357 12.03349 12.02519 12.01687 12.00681 11.99529 11.98262
## [481] 11.96910 11.95501 11.94066 11.92634 11.91235 11.89899 11.88654 11.87532
## [489] 11.86561 11.85771 11.85192 11.84853 11.84665 11.84518 11.84412 11.84348
## [497] 11.84325 11.84343 11.84404 11.84507 11.84652 11.84841 11.85071 11.85345
## [505] 11.85663 11.86023 11.86428 11.86876 11.87368 11.87905 11.88486 11.89236
## [513] 11.90261 11.91532 11.93022 11.94704 11.96550 11.98533 12.00624 12.02797
## [521] 12.05022 12.07274 12.09524 12.11745 12.13909 12.15988 12.17955 12.19783
## [529] 12.21443 12.22908 12.24150 12.25142 12.26117 12.27312 12.28702 12.30263
## [537] 12.31973 12.33806 12.35740 12.37751 12.39814 12.41906 12.44003 12.46081
## [545] 12.48117 12.50087 12.51966 12.53732 12.55360 12.56827 12.58108 12.59180
## [553] 12.60020 12.60603 12.60905 12.60969 12.60863 12.60603 12.60203 12.59679
## [561] 12.59047 12.58321 12.57518 12.56653 12.55740 12.54796 12.53835 12.52874
## [569] 12.51927 12.51011 12.50139 12.49328 12.48594 12.47950 12.47330 12.46655
## [577] 12.45929 12.45153 12.44330 12.43463 12.42554 12.41606 12.40620 12.39600
## [585] 12.38547 12.37465 12.36356 12.35222 12.34066 12.32890 12.31683 12.30435
## [593] 12.29146 12.27817 12.26447 12.25038 12.23591 12.22104 12.20580 12.19019
## [601] 12.17421 12.15787 12.14118 12.12413 12.10674 12.08901 12.07094 12.05255
## [609] 12.03383 12.01480 11.99545 11.97579 11.95581 11.93551 11.91490 11.89397
## [617] 11.87271 11.85114 11.82925 11.80703 11.78450 11.76164 11.73845 11.71494
## [625] 11.69111 11.66695 11.64246 11.61765 11.59251 11.56703 11.54123 11.51510
## [633] 11.48863 11.46184 11.43471 11.40724 11.37944 11.35131 11.32284
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")